Google Cloud Platform VM個體使用 - 2

一、透過SSH連線

  1. 點選SSH進行連線,透過SSH連線僅能透過終端機輸入指令操作,後續會教如何藉由Google遠端桌面進行連線
    image
  2. 點選Authorize
    image
  3. SSH介面
    image

二、透過Google遠端桌面連線

  1. 打開Google遠端桌面
  2. 點選透過SSH設定->開始
    image
  3. 點選繼續->授權
    image

    image
  4. 複製Linux的部分
    image
  5. 將複製的指令貼至SSH
    image
  6. 即可在遠端存取中看到可遠端的電腦
    image

三、安裝Apache網頁伺服器

已下指令皆在SSH中輸入

  1. 更新套件庫
    ​​​​sudo apt update
    
  2. 安裝Apache
    ​​​​sudo apt install apache2
    
  3. 啟動Apache
    ​​​​sudo systemctl status apache2
    
  4. 設定開機啟動
    ​​​​sudo systemctl enable apache2
    
  5. 查看Apache伺服器狀態
    ​​​​sudo systemctl status apache2
    

四、安裝MySQL

  1. 更新套件庫
    ​​​​sudo apt update
    
  2. 安裝MySQL
    ​​​​sudo apt install mysql-server
    
  3. 啟動MySQL
    ​​​​sudo systemctl status mysql
    
  4. 設定開機啟動
    ​​​​sudo systemctl enable mysql
    
  5. 查看MySQL伺服器狀態
    ​​​​sudo systemctl status mysql
    
  6. 重啟MySQL
    ​​​​sudo systemctl restart mysql
    

五、設定MySQL

設定MySQL可對外連接

  1. 在終端機輸入下方指令

    ​​​​sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
    
  2. 修正bind-address

    ​​​​#預設:bind-address = 127.0.0.1
    ​​​​bind-address = 0.0.0.0
    

六、安裝MQTT伺服器

  1. 更新套件庫

    ​​​​sudo apt update
    
  2. 安裝MQTT伺服器及測試軟體

    ​​​​#mosquitto 是 MQTT 伺服器軟體
    ​​​​#mosquitto-clients 是用於測試 MQTT 伺服器的工具
    ​​​​sudo apt install mosquitto mosquitto-clients
    
  3. 啟動伺服器

    ​​​​sudo systemctl start mosquitto
    
  4. 重啟伺服器

    ​​​​sudo systemctl restart mosquitto
    
  5. 檢查伺服器狀態

    ​​​​sudo systemctl status mosquitto
    
  6. 設定開機自動啟動Server

    ​​​​sudo systemctl enable mosquitto sudo systemctl start mosquitto
    
  7. Linux系統Mosquitto伺服器相關設定

    ​​​​#各作業系統檔案位置
    ​​​​Linux:/etc/mosquitto/mosquitto.conf
    ​​​​macOS:/usr/local/etc/mosquitto/mosquitto.conf
    ​​​​Windows:C:\Program Files\mosquitto\mosquitto.conf
    
  8. 創建使用者(輸入完會要求輸入密碼)

    ​​​​sudo mosquitto_passwd -c /etc/mosquitto/passwd <username>
    
  9. mosquitto.conf配置選項

    ​​​​設置端口 listener 1883 默認是1883(非加密連線)
    
    ​​​​加密連線(尚未測試)listener 8883
    ​​​​cafile /path/to/ca.crt
    ​​​​certfile /path/to/server.crt
    ​​​​eyfile /path/to/server.key
    
  10. 配置密碼文件

    ​​​​password_file /etc/mosquitto/passwd
    
  11. 允許匿名訂閱訊息

    ​​​​allow_anonymous true
    

七、開始使用MQTT

  1. 發布訊息
    -h伺服器位置、-t發布的主題、-m訊息、-u使用者名稱、-P使用者密碼、-p prot
    ​​​​mosquitto_pub -h <伺服器位置> -t "test/topic" -m "Hello MQTT" -u <username> -P <password> -p <prot>
    
  2. 訂閱訊息
    -h伺服器位置、-t訂閱的主題、-u使用者名稱、-p使用者密碼
    ​​​​有使用者訂閱
    ​​​​mosquitto_sub -h <伺服器位置> -t "test/topic" -u <username> -p <password>
    
    ​​​​匿名訂閱
    ​​​​mosquitto_sub -h <伺服器位置> -t "test/topic"
    

八、啟用MQTT WebSocket

  1. 修改 mosquitto.conf 設定
    ​​​​listener 1883`指定MQTT的Port
    ​​​​protocol mqtt`指定1883是給未加密的MQTT使用
    ​​​​listener 8083`指定加密MQTT的Port
    ​​​​protocol websockets`啟用WebSocket服務
    ​​​​http_dir /var/www/html/mqtt/`指定Web的資料夾
    

九、PHP資料夾給予權限

  1. 設定資料夾權限
    ​​​​sudo chown -R ivm1110630:ivm1110630 /var/www/html/
    
  2. 重啟伺服器
    ​​​​sudo systemctl restart apache2
    

十、設定防火牆

  1. 設定GCP防火牆
    點擊左側->虛擬私有雲網路->防火牆->建立防火牆規則

    image

  2. 相關設定

    項目 設定值 備註
    名稱 demo 可自訂義
    目標 網路中的所有執行個體
    來源IPv4範圍 0.0.0.0/0
    TCP 3306 資料庫專用通訊埠

    image


延伸閱讀

  1. Google Cloud Platform VM個體使用 - 1
  2. Google Cloud Platform VM個體使用 - 2
  3. Arduino ESP32使用步驟
  4. ESP32-WROOM-32 30P 全彩LED模組
  5. ESP32使用DHT11溫濕度辨識結合LED燈
  6. ESP32實作溫濕度智慧監測結合DHT11及LED開發